//
//  HeadlinesViewController.swift
//  FengHuangNews
//
//  Created by 新宝 on 2018/12/21.
//  Copyright © 2018年 LaiShengCai. All rights reserved.
//

import UIKit
import Kingfisher
//声明一个闭包
typealias fererBlock = (_ titleString:String)->()

class HeadlinesViewController: UIViewController {
   
    
    var tableView:UITableView = UITableView()
    var dataArray:NSArray=NSArray()
    var refreshControl:UIRefreshControl=UIRefreshControl()
    let  reuseIdentifier="HeadlinesCell"
    //定义一个闭包属性
    var callBack :fererBlock!
        
  

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
       // self.loadData()
        
        self.callBack={title in
           // self.loadData()
        }
    }
    
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        let key = NSKeyValueChangeKey.newKey
        for dic in change! {
            if(dic.key==key){
                let nameString = dic.value as! String
                if(nameString=="头条"){
                    self.loadData()
                    print("字典12356--------%@",dic.value)
                }
            }
        }
    }
    
    private func setupUI() {
        tableView=UITableView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight), style: .plain)
        tableView.delegate=self as UITableViewDelegate
        tableView.dataSource=self as UITableViewDataSource
        tableView.contentInset=UIEdgeInsets(top: 0, left: 0, bottom: 120, right: 0)
       tableView.rowHeight=100
        tableView.register(SportTableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
        refreshControl=UIRefreshControl()
        tableView.addSubview(refreshControl)
        self.view.addSubview(tableView)
    }
    
    
    private func loadData(){
        NetworkTool.loadAllData(controllerTitle: "头条") { (headlinesArr) in
            self.dataArray=headlinesArr as NSArray
            self.tableView.reloadData()
        }
        
        self.refreshControl.addTarget(self, action: #selector(self.refreshData), for: UIControl.Event.valueChanged)
        refreshControl.attributedTitle=NSAttributedString(string: "下拉刷新数据")
    }
    
    @objc func refreshData(){
        NetworkTool.loadAllData(controllerTitle: "头条") { (headlinesArr) in
            self.dataArray=headlinesArr as NSArray
            self.tableView.reloadData()
            self.refreshControl.endRefreshing()
        }
    }
   
}



extension HeadlinesViewController:UITableViewDelegate,UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = SportTableViewCell(style: .default, reuseIdentifier: reuseIdentifier)
        var headlinesModel=HeadlinesModel()
        headlinesModel=self.dataArray[indexPath.row] as! HeadlinesModel
        let urlString = "http://d.ifengimg.com/w198_h141_q100/p0.ifengimg.com/pmop/2017/0613/E35D45FD500E2A2C116D6B8229094675D634C580_size33_w551_h530.jpeg"
        let url=NSURL(string:(headlinesModel.thumbnail?.isEmpty)! ? urlString : headlinesModel.thumbnail!)
        // 设置网络图片
        cell.headImageView.kf.setImage(with: ImageResource(downloadURL: url! as URL))
        cell.titleLabel.text=headlinesModel.title
        cell.timeLabel.text=headlinesModel.updateTime
        cell.selectionStyle = UITableViewCell.SelectionStyle.none;
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        var headlinesModel=HeadlinesModel()
        headlinesModel=dataArray[indexPath.row] as! HeadlinesModel
        let webVC=WebViewController()
        webVC.urlString=headlinesModel.commentsUrl
        self.present(webVC, animated: true, completion: nil)
    }
}
